package com.yt.sys.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import com.yt.sys.models.service.IEmployeeInfoService;
import com.yt.sys.models.bo.EmployeeInfoBO;
import com.yt.sys.models.dto.EmployeeInfoDTO;
import com.yt.sys.models.bo.EmployeeInfoSaveBO;

import java.util.List;

/**
 * 员工信息控制器
 *
 * @author yzz
 * @Date 2025-10-22 09:44:20
 */

@RestController
@RequestMapping("/api/employeeInfo")
@Slf4j
@Api(value = "EmployeeInfoApiController",tags = "员工信息Api接口")
public class EmployeeInfoApiController {

    @DubboReference
    private IEmployeeInfoService employeeInfoService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "String")
    public Result<EmployeeInfoDTO> get(@RequestParam("id") String id){
        if(id == null){
            Result<EmployeeInfoDTO> result = new Result<>();
            result.setCode(CodeEnum.FAIL_100002.getCode());
            result.setMsg(CodeEnum.FAIL_100002.getMsg());
            return result;
        }

        // 使用Wrapper查询来支持String类型的ID
        Wrapper<EmployeeInfoDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("employee_id", id);
        EmployeeInfoDTO dto = employeeInfoService.selectOne(wrapper);

        Result<EmployeeInfoDTO> result = new Result<>();
        result.setData(dto);
        return result;
    }

    @ApiOperation(value = "员工信息列表",notes = "员工信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<EmployeeInfoDTO>> queryPageList(EmployeeInfoBO bo){
         Wrapper<EmployeeInfoDTO> wrapper = new EntityWrapper<>();
                if (bo.getStatus() != null){
                    wrapper.eq("status", bo.getStatus());
                }
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<EmployeeInfoDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = employeeInfoService.selectPage(page, wrapper);

        Result<Page<EmployeeInfoDTO>> result = new Result<>();
        result.setData(page);
        return result;
    }
    
    @ApiOperation(value = "获取所有员工信息", notes = "获取所有员工信息")
    @PostMapping ("/listAll")
    public Result<List<EmployeeInfoDTO>> listAll() {
        // 修改为使用Wrapper查询，以正确处理varchar类型的员工ID
        Wrapper<EmployeeInfoDTO> wrapper = new EntityWrapper<>();
        List<EmployeeInfoDTO> employeeList = employeeInfoService.selectList(wrapper);
        
        Result<List<EmployeeInfoDTO>> result = new Result<>();
        result.setData(employeeList);
        return result;
    }
    
    @ApiOperation(value = "新增员工信息", notes = "新增员工信息")
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody EmployeeInfoSaveBO saveBO) {
        try {
            Result<Boolean> result = new Result<>();
            // 参数校验
            if (saveBO == null) {
                result.setData(false);
                result.setCode(CodeEnum.FAIL_100002.getCode());
                result.setMsg("参数不能为空");
                return result;
            }
            
            // 执行新增操作
            boolean operationResult = employeeInfoService.add(saveBO);
            if (operationResult) {
                result.setData(true);
                result.setCode(CodeEnum.SUCCESS.getCode());
                result.setMsg("新增员工成功");
            } else {
                result.setData(false);
                result.setCode(CodeEnum.FAIL.getCode());
                result.setMsg("新增员工失败");
            }
            return result;
        } catch (Exception e) {
            log.error("新增员工信息异常", e);
            Result<Boolean> result = new Result<>();
            result.setData(false);
            result.setCode(CodeEnum.FAIL.getCode());
            result.setMsg("新增员工异常：" + e.getMessage());
            return result;
        }
    }
    
    @ApiOperation(value = "修改员工信息", notes = "修改员工信息")
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody EmployeeInfoSaveBO saveBO) {
        try {
            Result<Boolean> result = new Result<>();
            // 参数校验
            if (saveBO == null || saveBO.getId() == null || saveBO.getId().isEmpty()) {
                result.setData(false);
                result.setCode(CodeEnum.FAIL_100002.getCode());
                result.setMsg("参数不能为空，员工ID不能为空");
                return result;
            }
            
            // 先查询员工信息是否存在
            Wrapper<EmployeeInfoDTO> wrapper = new EntityWrapper<>();
            wrapper.eq("employee_id", saveBO.getId());
            EmployeeInfoDTO existingEmployee = employeeInfoService.selectOne(wrapper);
            
            if (existingEmployee == null) {
                result.setData(false);
                result.setCode(CodeEnum.FAIL.getCode());
                result.setMsg("员工信息不存在");
                return result;
            }
            
            // 执行更新操作
            boolean operationResult = employeeInfoService.update(saveBO);
            if (operationResult) {
                result.setData(true);
                result.setCode(CodeEnum.SUCCESS.getCode());
                result.setMsg("修改员工信息成功");
            } else {
                result.setData(false);
                result.setCode(CodeEnum.FAIL.getCode());
                result.setMsg("修改员工信息失败");
            }
            return result;
        } catch (Exception e) {
            log.error("修改员工信息异常", e);
            Result<Boolean> result = new Result<>();
            result.setData(false);
            result.setCode(CodeEnum.FAIL.getCode());
            result.setMsg("修改员工信息异常：" + e.getMessage());
            return result;
        }
    }
    
    @ApiOperation(value = "删除员工信息", notes = "删除员工信息")
    @PostMapping("/delete")
    @ApiImplicitParam(name = "id", value = "员工ID", required = true, paramType = "query", dataType = "String")
    public Result<Boolean> delete(@RequestParam("id") String id) {
        try {
            Result<Boolean> result = new Result<>();
            // 参数校验
            if (id == null || id.isEmpty()) {
                result.setData(false);
                result.setCode(CodeEnum.FAIL_100002.getCode());
                result.setMsg("参数不能为空，员工ID不能为空");
                return result;
            }
            
            // 先查询员工信息是否存在
            Wrapper<EmployeeInfoDTO> queryWrapper = new EntityWrapper<>();
            queryWrapper.eq("employee_id", id);
            EmployeeInfoDTO existingEmployee = employeeInfoService.selectOne(queryWrapper);
            
            if (existingEmployee == null) {
                result.setData(false);
                result.setCode(CodeEnum.FAIL.getCode());
                result.setMsg("员工信息不存在");
                return result;
            }
            
            // 执行删除操作
            Wrapper<EmployeeInfoDTO> deleteWrapper = new EntityWrapper<>();
            deleteWrapper.eq("employee_id", id);
            boolean operationResult = employeeInfoService.delete(deleteWrapper);
            
            if (operationResult) {
                result.setData(true);
                result.setCode(CodeEnum.SUCCESS.getCode());
                result.setMsg("删除员工信息成功");
            } else {
                result.setData(false);
                result.setCode(CodeEnum.FAIL.getCode());
                result.setMsg("删除员工信息失败");
            }
            return result;
        } catch (Exception e) {
            log.error("删除员工信息异常", e);
            Result<Boolean> result = new Result<>();
            result.setData(false);
            result.setCode(CodeEnum.FAIL.getCode());
            result.setMsg("删除员工信息异常：" + e.getMessage());
            return result;
        }
    }
    
    @ApiOperation(value = "根据员工ID或姓名查询员工", notes = "根据员工ID或姓名查询员工，支持模糊查询")
    @PostMapping("/search")
    public Result<List<EmployeeInfoDTO>> search(@RequestParam(value = "keyword", required = true) String keyword) {
        try {
            Result<List<EmployeeInfoDTO>> result = new Result<>();
            // 参数校验
            if (keyword == null || keyword.trim().isEmpty()) {
                result.setCode(CodeEnum.FAIL_100002.getCode());
                result.setMsg(CodeEnum.FAIL_100002.getMsg());
                return result;
            }
            
            // 构造查询条件
            Wrapper<EmployeeInfoDTO> wrapper = new EntityWrapper<>();
            wrapper.like("employee_id", keyword).or().like("name", keyword);
            
            // 执行查询
            List<EmployeeInfoDTO> employeeList = employeeInfoService.selectList(wrapper);
            
            result.setData(employeeList);
            return result;
        } catch (Exception e) {
            log.error("根据员工ID或姓名查询员工异常", e);
            Result<List<EmployeeInfoDTO>> result = new Result<>();
            result.setCode(CodeEnum.FAIL.getCode());
            result.setMsg("查询异常：" + e.getMessage());
            return result;
        }
    }
}